//
//  FZTabBar.swift
//  MyWeiBo
//
//  Created by fangzhengwei on 2017/3/26.
//  Copyright © 2017年 fangzhengwei. All rights reserved.
//

import UIKit

class FZTabBar: UITabBar {

    var flagClosure : (()->())?
    
    lazy var addBtn : UIButton = {
        
        let btn = UIButton()
        btn.setBackgroundImage(UIImage(named: "tabbar_compose_button"), for: .normal)
        btn.setBackgroundImage(UIImage(named: "tabbar_compose_button_highlighted"), for: .highlighted)
        btn.setImage(UIImage(named:"tabbar_compose_icon_add"), for: .normal)
        btn.setImage(UIImage(named:"tabbar_compose_icon_add_highlighted"), for: .highlighted)
        btn.sizeToFit()
        btn.addTarget(self, action: #selector(btnClick), for: .touchUpInside)
        return btn
    }()
    
    
    override init(frame: CGRect) {
        
        super.init(frame: frame)
        
        addSubview(addBtn)
        
    }
    required init?(coder aDecoder: NSCoder) {
        fatalError("init(coder:) has not been implemented")
    }
    //MARK:闭包回调 //不让外界访问这个方法
    @objc private func btnClick() { 
        
        flagClosure?()
    }

}



extension FZTabBar {
    
    override func layoutSubviews() {
        super.layoutSubviews()
        let itemWidth = self.bounds.size.width / 5
        
        addBtn.center.x = self.bounds.size.width * 0.5
        addBtn.center.y = self.bounds.size.height * 0.5
        
        var index : Int = 0
        
        for subView in subviews{
            
            if subView.isKind(of: NSClassFromString("UITabBarButton")!) {
                
                subView.bounds.size.width = itemWidth
                
                if index == 2 {
                   index += 1
                }
                
                subView.frame.origin.x = CGFloat(index) * itemWidth
                
                index += 1
                
            }
        }
        
    }
}





